var e = getApp(), a = e.require("utils/onfire.js"), t = e.require("utils/util.js"), n = e.require("utils/api.js"), i = e.require("utils/enums.js"), o = null, r = {}, s = null, u = -1, l = null, d = null, p = null, f = -1, c = [], m = null, h = {
    Logs: []
};

Page({
    data: {
        errandWeights: i.ErrandWeights,
        giveupCauses: s,
        appraiseLabels: p,
        appraiseLabelsGroup: f,
        appraiseLabelsIndex: c,
        appraiseInputFocus: !1,
        timeRemain: "",
        submitAction: null,
        orderStatus: null,
        model: h,
        modalType: "none",
        popupType: "none",
        dialogType: "none",
        dialogMessage: null,
        fees: o,
        feeIndex: 0,
        feeInputFocus: !1,
        feeInputValue: 0
    },
    onLoad: function(e) {
        var a = this;
        o = t.makeArray(1, 6), s = [ "下单人主动要求取消", "订单描述与实际情况不符", "临时有事，没时间继续跑腿" ], p = [ [ "准时送达", "货品完好", "送货快", "颜值高", "礼貌热情", "任务保质保量" ], [ "马马乎乎吧", "包装破损了，但并无大碍" ], [ "配送超慢", "态度贼差", "联系沟通困难", "送达不通知", "未按要求完成" ] ], 
        a.setData({
            giveupCauses: s,
            fees: o,
            appraiseLabels: p
        }), l = null, r = e, a.loadData(e);
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {
        m && clearInterval(m);
        var e = getCurrentPages();
        e.length > 1 && e[e.length - 2].route.indexOf("errand/apply/index") >= 0 && wx.navigateBack();
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    loadData: function(e) {
        var o = this;
        if (e && e.status) {
            h.Status = e.status;
            var s = t.indexOfArray(i.OrderStatus, function(a) {
                return a.Value == e.status;
            });
            o.setData({
                model: h,
                timeRemain: "--:--",
                orderStatus: s >= 0 ? i.OrderStatus[s] : null
            });
        }
        n.orderInfo({
            id: e && e.id || r.id
        }, function(n) {
            h = n, e && n.Status != e.status && a.fire("refreshHomeOrders");
            var r = t.indexOfArray(i.OrderStatus, function(e) {
                return e.Value == n.Status;
            });
            if (o.setData({
                model: h,
                orderStatus: r >= 0 ? i.OrderStatus[r] : null
            }), h.Status <= 1 && h.PaymentLimitSeconds > 0) {
                var s = 0;
                m = setInterval(function() {
                    var e = h.PaymentLimitSeconds - s, a = parseInt(e / 60), t = parseInt(e % 60);
                    o.setData({
                        timeRemain: e > 0 ? (a >= 10 ? a : "0" + a) + ":" + (t >= 10 ? t : "0" + t) : null
                    }), m && h.PaymentLimitSeconds - s <= 0 ? (clearInterval(m), o.loadData()) : s++;
                }, 1e3);
            }
        });
    },
    onStatusTap: function() {
        var e = this;
        e.data.model.Role > 0 && e.data.model.Role < 3 && this.setData({
            popupType: "track"
        });
    },
    onCancelTap: function() {
        var e = this;
        wx.showModal({
            title: "取消确认",
            confirmColor: "#3bbb8d",
            content: "确定要取消订单？",
            success: function(a) {
                a.confirm && n.orderCancel({
                    id: r.id
                }, function() {
                    e.loadData();
                });
            }
        });
    },
    onModalConfirm: function() {
        var e = this;
        n.orderRefund({
            id: r.id
        }, function() {
            a.fire("refreshHomeOrders"), e.loadData();
        }), e.setData({
            modalType: "none"
        });
    },
    onModalCancel: function() {
        this.setData({
            modalType: "none"
        });
    },
    onRequestRefundTap: function() {
        this.setData({
            dialogType: "profile",
            dialogMessage: "请先联系猎人放弃订单，然后再取消"
        });
    },
    onRefundTap: function() {
        var e = this;
        e.setData({
            modalType: "refund"
        });
    },
    onRaiseTap: function() {
        this.setData({
            popupType: "tip"
        });
    },
    onGiveupContentChanged: function(e) {
        l = e.detail.value;
    },
    onGiveupCauseChange: function(e) {
        u = e.detail.value;
    },
    onGiveupTap: function() {
        this.setData({
            popupType: "giveup"
        });
    },
    onFinishTap: function() {
        this.setData({
            dialogType: "finish"
        });
    },
    onFinishConfirm: function() {
        var e = this;
        n.orderFinish({
            id: r.id
        }, function() {
            e.setData({
                dialogType: "none"
            }), a.fire("refreshHomeOrders"), e.loadData();
        });
    },
    onFeeItemTap: function(e) {
        var a = this, t = e.currentTarget.dataset.index;
        a.setData({
            feeIndex: t,
            feeInputFocus: t < 0
        });
    },
    onFeeInputBlur: function(e) {
        var a = this, t = e.detail.value;
        t && t.length && (t = parseInt(t), a.setData({
            feeIndex: o.indexOf(t),
            feeInputFocus: o.indexOf(t) < 0,
            feeInputValue: o.indexOf(t) < 0 ? t : 0
        }));
    },
    onPopupConfirm: function() {
        var e = this, i = e.data.popupType;
        setTimeout(function() {
            if ("tip" == i) {
                if (e.data.feeIndex < 0 && !e.data.feeInputValue) return void t.toast("请输入小费金额");
            } else if ("giveup" == i) {
                if (u < 0) return void t.toast("请选择放弃订单原因");
                if (!(u != s.length || l && l.length)) return void t.toast("请输入原因说明");
            }
            e.setData({
                popupType: "none"
            }), setTimeout(function() {
                if ("tip" == i) {
                    var m = e.data.feeIndex < 0 ? e.data.feeInputValue >= 1 ? Math.floor(e.data.feeInputValue) : 1 : o[e.data.feeIndex];
                    n.orderPayment({
                        id: r.id,
                        money: m
                    }, function(a) {
                        e.loadData();
                    });
                } else if ("giveup" == i) n.orderGiveup({
                    id: r.id,
                    cause: (u >= 0 && u < s.length - 1 ? s[u] + "。" : "") + l
                }, function() {
                    a.fire("refreshHomeOrders"), h.Status = 512, h.Logs.splice(0, 0, {
                        Title: "赏金猎人取消订单",
                        CreatedTime: t.formatDate(new Date(), "s")
                    }), e.setData({
                        orderStatus: {
                            Name: "放弃跑腿",
                            Value: 512
                        },
                        model: h
                    });
                }); else if ("appraise" == i) {
                    var v = [];
                    if (f >= 0 && c.length) for (var g in c) {
                        var D = c[g];
                        v.push(p[f][D]);
                    }
                    n.orderComment({
                        id: r.id,
                        content: d || "",
                        labels: v.join(",")
                    }, function() {
                        e.loadData(), a.fire("refreshHomeOrders");
                    });
                }
            }, 200);
        }, 50);
    },
    onPopupCancel: function() {
        this.setData({
            popupType: "none"
        });
    },
    onAvatarTap: function() {
        this.setData({
            dialogType: "profile",
            dialogMessage: null
        });
    },
    onPhoneTap: function() {
        wx.makePhoneCall({
            phoneNumber: 1 == h.Role ? h.DelivererMobile : h.Mobile
        });
    },
    onDialogClose: function() {
        this.setData({
            dialogType: "none"
        });
    },
    onPaymentSubmit: function(e) {
        var t = this;
        h.Status <= 1 && t.data.timeRemain && n.orderPayment({
            id: r.id
        }, function(e) {
            t.loadData(), a.fire("refreshHomeOrders");
        });
    },
    onAppraiseContentChanged: function(e) {
        d = e.detail.value;
    },
    onAppraiseLabelTap: function(e) {
        var a = this, t = e.currentTarget.dataset.group, n = e.currentTarget.dataset.index;
        if (f < 0) f = t, c.push(n); else if (f == t) {
            var i = c.indexOf(n);
            i < 0 ? c.push(n) : (c.splice(i, 1), 0 == c.length && (f = -1));
        }
        a.setData({
            appraiseLabelsIndex: c,
            appraiseLabelsGroup: f
        });
    },
    onAppraiseSubmit: function(e) {
        n.userSaveFormId({
            formId: e.detail.formId
        }), this.setData({
            popupType: "appraise"
        });
    }
});